<template>
  <div>
    <div class="head">
      <div class="input">
        <span>课程名称：</span>
        <el-input v-model="queryData.title" placeholder="请输入" />
      </div>
      <div class="input">
        <span>企业：</span>
        <el-select v-model="queryData.company_id" placeholder="请选择企业">
          <el-option v-for="(item, index) in companies" :key="index" :label="item.company_name" :value="item.id">{{item.company_name}}</el-option>
        </el-select>
      </div>
      <div class="input">
        <span>培训类型：</span>
        <el-cascader
          :show-all-levels="false"
          :options="options2"
          v-model="queryData.selected_type_ids"
          :props="{ multiple: true, checkStrictly: true }"
          @change="handleChange"
          clearable
        ></el-cascader>
      </div>
      <el-button type="primary" class="btn" @click="search">查询</el-button>
      <el-button type="primary" class="btn" @click="reset">重置</el-button>
      <el-button type="primary" class="btn" @click="add">新增课件</el-button>
    </div>
    <div class="box">
      <el-table
        :data="tableData"
        border
        v-loading="isTableLoading"
        empty-text="暂无数据"
        no-filtered-userFrom-text="暂无筛选结果"
        style="width: 100%"
      >
        <el-table-column fixed type="index" align="center" label="序号" min-width="50" />
        <el-table-column fixed prop="title" align="center" show-overflow-tooltip label="课件名称" min-width="180" />
        <el-table-column prop="company_name" align="center" label="企业" min-width="100" />
        <el-table-column prop="type_name" align="center" label="培训类型" min-width="180"></el-table-column>
        <el-table-column prop="teacher.username" align="center" label="讲师姓名" min-width="120"></el-table-column>
        <el-table-column prop="duration" align="center" label="课长" min-width="100" />
        <el-table-column prop="create_time" align="center" label="创建时间" min-width="140"></el-table-column>
        <el-table-column fixed="right" align="center" prop="action" label="操作" min-width="160">
          <template #default="scope">
            <div class="action">
              <!--<span @click="edit(scope, 2)">预览</span>-->
              <span @click="edit(scope, 1)">编辑</span>
              <span @click="del(scope)">删除</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="acea-row row-right page">
        <el-pagination
          style="margin-top: 5px"
          @size-change="sizeSearch"
          @current-change="search"
          :current-page.sync="queryData.page"
          :page-size.sync="queryData.size"
          layout="total,sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <EditCourseware ref="edit"></EditCourseware>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>确定删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="delOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import EditCourseware from './edit.vue'
import request from '@/libs/request';

export default {
  name: 'questionCoursewareIndex',
  components: {EditCourseware},
  data() {
    return {
      queryData: {
        title: '',
        company_id: '',
        type_id: [],
        selected_type_ids: [],
        page: 1,
        size: 10,
      },
      options2: [],
      total: 0,
      isTableLoading: false,
      tableData: [],
      companies: [],
      dialogVisible: false,
    };
  },
  computed: {},
  created() {},
  mounted() {
    this.getList();
    this.getType();
  },
  methods: {
    search(d) {
      if (typeof d == 'number') {
        this.queryData.page = d;
      } else {
        this.queryData.page = 1;
      }
      this.getList();
    },
    sizeSearch(d) {
      this.queryData.size = d;
      this.search();
    },
    reset() {
      this.queryData.title = '';
      this.queryData.company_id = '';
      this.queryData.selected_type_ids = [];
      this.queryData.type_id = [];
      this.search();
    },
    edit(item, type) {
      this.$refs.edit.showModal(item.row.id)
    },
    del(item) {
      // this.dialogVisible = true;
      this.item = item;
      this.$confirm('确定要删除课件吗？', '提醒', {
        type: 'warning',
      }).then(res => {
        this.delOk()
      }).catch(err => {
      })
    },
    delOk() {
      let loading = this.$loading()
      request({
        url: '/courseware/delete',
        method: 'POST',
        data: {id: this.item.row.id}
      }).then(res => {
        loading.close();
        this.$delete(this.tableData, this.item.$index);
        this.dialogVisible = false;
      }).catch(err => {
        this.$message.error(err.msg)
        loading.close()
        this.dialogVisible = false;
      })
    },
    add() {
      this.$refs.edit.showModal()
    },
    getList() {
      this.isTableLoading = true;
      let loading = this.$loading()
      request({
        url: '/courseware/list',
        method: 'post',
        data: this.queryData,
      }).then(res => {
        loading.close()
        this.tableData = res.data.list
        this.total = res.data.total;
        this.isTableLoading = false;
      }).catch(err => {
        this.isTableLoading = false;
        loading.close()
      })
    },
    getType() {
      request({
        url: '/traintype/list',
        method: 'post',
      }).then((res) => {
        this.companies = res.data.companies
        this.options2 = res.data.list
      });
    },
    handleChange(value) {
      this.queryData.type_id = []
      if (value) {
        for (let item of value) {
          this.queryData.type_id.push(...item)
        }
      }
      this.queryData.type_id = Array.from(new Set(this.queryData.type_id))
    },
  },
};
</script>

<style scoped>
.head {
  width: 100%;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  background: #ffffff;
  border-radius: 5px;
  padding: 20px;
  padding-bottom: 0px;
}

.input {
  display: flex;
  align-items: center;
  margin-right: 15px;
  margin-bottom: 20px;
}

.input > span {
  font-size: 12px;
  flex-shrink: 0;
}

.btn {
  margin-bottom: 20px;
}

.box {
  width: 100%;
  background: #ffffff;
  border-radius: 5px;
  padding: 20px;
  margin-top: 20px;
}

.action {
  display: flex;
  align-items: center;
  justify-content: center;
}

.action span {
  color: #0256ff;
  font-size: 12px;
  cursor: pointer;
  margin: 0 8px;
}
</style>
